/*
 * Copyright (c) MengChang Wang <wangmengchang@gmail.com>
 * Webpage:  http://www.journalhome.com/texpen/
 *
 *
*/

#include "headers/mainwindow.h"
#include <QAction>
#include <QTextDocument>
#include <QTextCursor>
#include <QVector>
#include <QDebug>

QString eqRec="";

bool MainWindow::isInlineEquation(){
    QTextCursor cs = TextEdit->textCursor();
    int pos = cs.position();

    QTextCursor st = TextEdit->document()->find("$");
    if(st.isNull()){
        return false;
    }
        while(isTexComment(st)){
            st = TextEdit->document()->find("$",st.position()+1);
            if(st.isNull()){
                return false;
            }
        }
    QTextCursor et = TextEdit->document()->find("$",st.position()+1);
    if(et.isNull()){
        return false;
    }
        while(isTexComment(et)){
            et = TextEdit->document()->find("$",et.position()+1);
            if(et.isNull()){
                return false;
            }
        }
   //

        while(et.position() - st.position() <2
              || (st.position()<pos && et.position() < pos)

              ){
            st = TextEdit->document()->find("$",et.position()+1);
            if(st.isNull()){
                return false;
            }
                    while(isTexComment(st)){
                        st = TextEdit->document()->find("$",st.position()+1);
                        if(st.isNull()){
                            return false;
                        }
                    }
            et = TextEdit->document()->find("$",st.position()+1);
            if(et.isNull()){
                return false;
            }
                    while(isTexComment(et)){
                        et = TextEdit->document()->find("$",et.position()+1);
                        if(et.isNull()){
                            return false;
                        }
                    }
        }
        //
        if(et.position() - st.position() >= 2
                && (st.position()<pos && et.position() > pos)){
            eqRec=TextEdit->toPlainText().mid(st.position(), et.position() - st.position());
            return true;
        }
        return false;

}


bool MainWindow::isNowEquation() //judge wheather the cursor is in an equation
{
    eqRec="";
    //qDebug()<<"isNowEquation()"<<endl;
    QTextCursor cs = TextEdit->textCursor();
    int pos = cs.position(); //qDebug()<<"\tpos ="<<pos<<endl;
    QTextCursor st = TextEdit->document()->find("\\begin{equation}");
    if(st.isNull()){
        return false;
    }
        while(isTexComment(st)){
            st = TextEdit->document()->find("\\begin{equation}",st.position()+1);
            if(st.isNull()){
                return false;
            }
        }
    QTextCursor et = TextEdit->document()->find("\\end{equation}",st.position());
    if(et.isNull()){
            return false;
    }
        while(isTexComment(et)){
            et = TextEdit->document()->find("\\begin{equation}",et.position()+1);
            if(et.isNull()){
                break;
            }
        }
    if(st.isNull()){
        return false;
    }else if (et.isNull()){
        return false;
    }
    while(st.position() < pos && et.position()< pos){

        st = TextEdit->document()->find("\\begin{equation}",et.position() +1 );
                while(isTexComment(st)){
                    st = TextEdit->document()->find("\\begin{equation}",st.position()+1);
                    if(st.isNull()){
                        return false;
                    }
                }
        et = TextEdit->document()->find("\\end{equation}",st.position());
                while(isTexComment(et)){
                    et = TextEdit->document()->find("\\begin{equation}",et.position()+1);
                    if(et.isNull()){
                        break;
                    }
                }

        if( st.isNull() || et.isNull() ){
            return false;
        }
    }
    if(st.position() < pos && et.position()> pos){
        eqRec=TextEdit->toPlainText().mid(st.position(), et.position() - st.position());
        return true;
    }
    return false;
}

bool MainWindow::isNowEquation(QString sts, QString eds){
     QTextCursor cs = TextEdit->textCursor();
     int pos = cs.position();
     QTextCursor st = TextEdit->document()->find(sts);
     if(st.isNull()){
         return false;
     }
     while(isTexComment(st)){
         st = TextEdit->document()->find(sts,st.position()+1);
         if(st.isNull()){
             return false;
         }
     }

     QTextCursor et = TextEdit->document()->find(eds,st.position());
     if(et.isNull()){
             return false;
     }
     while(isTexComment(et)){
         et = TextEdit->document()->find(sts,et.position()+1);
         if(et.isNull()){
             break;
         }
     }

     if(st.isNull()){
         return false;
     }else if (et.isNull()){
         return false;
     }
     while(st.position() < pos && et.position()< pos){
         st = TextEdit->document()->find(sts,et.position() +1 );
                 while(isTexComment(st)){
                     st = TextEdit->document()->find(sts,st.position()+1);
                     if(st.isNull()){
                         return false;
                     }
                 }
         et = TextEdit->document()->find(eds,st.position());
                 while(isTexComment(et)){
                     et = TextEdit->document()->find(sts,et.position()+1);
                     if(et.isNull()){
                         break;
                     }
                 }

         if( st.isNull() || et.isNull() ){
             return false;
         }
     }
     if(st.position() < pos && et.position()> pos){
         eqRec=TextEdit->toPlainText().mid(st.position(), et.position() - st.position());
         return true;
     }
     return false;
}




QString MainWindow::equationString()//get the equation source
{//remove comments //remove labels
    QString rs = eqRec;
    rs.remove(QRegExp("\\\\label\\{*\\}"));
    rs.remove(QRegExp("\%[^\n]*"));
    rs.remove("\\end{equation}");
    rs.replace("\n", " ");//remove("\n");

    rs.remove("\\]");
    rs.remove("\\[");
    rs.remove("$$");
    if(!rs.endsWith("\\$")){
        if(rs.endsWith("$")){
            int l=rs.length();
            rs.remove(l-1,1);
        }
    }

    if(rs.endsWith("\\end{flalign}")){
        rs = "\\begin{flalign}" + rs;
    }else if(rs.endsWith("\\end{flalign*}")){
        rs = "\\begin{flalign*}" + rs;
    }else if(rs.endsWith("\\end{align}")){
        rs = "\\begin{align}" + rs;
    }else if(rs.endsWith("\\end{align*}")){
        rs = "\\begin{align*}" + rs;
    }else if(rs.endsWith("\\end{gather}")){
        rs = "\\begin{gather}" + rs;
    }else if(rs.endsWith("\\end{multline}")){
        rs = "\\begin{multline}" + rs;
    }
    return rs;
}

void MainWindow::renderEquation()//call api-s to render this source into images
{

    eqRec="";
    if( isNowEquation("$$","$$") ||
            isNowEquation("\\begin{equation}","\\end{equation}") ||
            isNowEquation("\\[","\\]")||
            isNowEquation("\\begin{flalign}","\\end{flalign}")||
            isNowEquation("\\begin{flalign*}","\\end{flalign*}")||
            isNowEquation("\\begin{align}","\\end{align}")||
            isNowEquation("\\begin{align*}","\\end{align*}")||
            isNowEquation("\\begin{gather}","\\end{gather}")||
            isNowEquation("\\begin{multline}","\\end{multline}")||
            isInlineEquation()
            ){

        equationview->setWindowTitle("Preview");
        QString ur = "http://latex.numberempire.com/render?";
        ur += equationString();
        //QString ur = "http://chart.apis.google.com/chart?cht=tx&chl=";
        //ur += QUrl::toPercentEncoding(equationString());
        equation->setHtml(QString::fromUtf8("<center><img src=\"%1\" align=\"center\"></center>").arg(ur)
                          );
        equationview->show();
        TextEdit->centerCursor();
    }else{
        equationview->hide();
    }
}

//Table ---> Preview


//Figure ---> Preview






